//
//  MineController.m
//  HB
//
//  Created by 孙智慧 on 16/1/11.
//  Copyright © 2016年 孙智慧. All rights reserved.
//

#import "MineController.h"
#import "MusicListTableViewCell.h"
#import "LoginOperation.h"
#import "SetController.h"
#import "ShowMusicListController.h"
#import "DownLoadInfoController.h"
#import "AuthenticationController.h"
#import "UserInfoController.h"
#import "PointController.h"

@implementation MineController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableMenu.delegate=self;
    
    self.tableMenu.dataSource= self;
    
    self.tableMenu.scrollEnabled=NO;
    
    self.tableSet.delegate=self;
    
    self.tableSet.dataSource= self;
    
    self.tableSet.scrollEnabled=NO;
    
    self.imgCheckView.hidden=YES;
    
    [self loadDJInfo];
    
    self.headImg.layer.cornerRadius=45;
    
    self.headImg.clipsToBounds=true;
    
    self.headImg.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *singleTap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openUserInfo:)];
    
    [self.headImg addGestureRecognizer:singleTap2];
    
    //毛玻璃
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle: UIBlurEffectStyleDark];
    
    UIVisualEffectView *visualEffectView= [[UIVisualEffectView alloc]initWithEffect:blurEffect];
    
    CGFloat width=  [UIScreen mainScreen].bounds.size.width;
    
    CGFloat height=self.bgImgView.bounds.size.height;
    
    visualEffectView.frame = CGRectMake(0.0, 0.0, width, height);
    
    visualEffectView.alpha = 1;
    
    [self.bgImgView addSubview:visualEffectView];
    
    RegisterNotify(NofifyMusicPlay, @selector(stopNavBtnRoation));
    
}

#pragma mark 加载用户信息
-(void) loadData:(NSString *)djID
{
    operation= [[LoginOperation alloc]initWithDelegate:self];
    
    NSString *url= [FxGlobal getServiceUrl:@"User" andFuncName:@"FansCount"];
    
    NSString *u= [NSString stringWithFormat:@"%@?p_djID=%@", url,djID];
    
    NSDictionary *dictInfo = @{@"url":u};
    
    [operation setOpInfo:dictInfo];
    
    [operation get:0];
    
    operation= [[LoginOperation alloc]initWithDelegate:self];
    
    NSString *url1= [FxGlobal getServiceUrl:@"User" andFuncName:@"MusicCount"];
    
    NSString *u1= [NSString stringWithFormat:@"%@?p_djID=%@", url1,djID];
    
    NSDictionary *dictInfo1 = @{@"url":u1};
    
    [operation setOpInfo:dictInfo1];
    
    [operation get:1];
    
    operation= [[LoginOperation alloc]initWithDelegate:self];
    
    NSString *url2= [FxGlobal getServiceUrl:@"User" andFuncName:@"FocusCount"];
    
    NSString *u2= [NSString stringWithFormat:@"%@?p_djID=%@", url2,djID];
    
    NSDictionary *dictInfo2 = @{@"url":u2};
    
    [operation setOpInfo:dictInfo2];
    
    [operation get:2];
    
    operation= [[LoginOperation alloc]initWithDelegate:self];
    
    NSString *url3= [FxGlobal getServiceUrl:@"User" andFuncName:@"DJInfo"];
    
    NSString *u3= [NSString stringWithFormat:@"%@?p_djID=%@", url3,djID];
    
    NSDictionary *dictInfo3 = @{@"url":u3};
    
    [operation setOpInfo:dictInfo3];
    
    [operation get:3];
    
}

-(void)opSuccessForGet:(id)data andFlag:(NSInteger)flag
{
    operation=nil;
    
    NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSDictionary *data1 = [dataStr objectFromJSONString];
    
    if (flag==0)
    {
        NSString *v=[data1 objectForKey:@"count"];
        
        dispatch_sync(dispatch_get_main_queue(), ^{
            
            self.lblFansCount.text=v;
        });
    }
    else if (flag==1)
    {
        NSString *v=[data1 objectForKey:@"count"];
        
        dispatch_sync(dispatch_get_main_queue(), ^{
            
            self.lblMusicCount.text=v;
        });
    }
    else if (flag==2)
    {
        NSString *v=[data1 objectForKey:@"count"];
        
        dispatch_sync(dispatch_get_main_queue(), ^{
            
            self.lblFocusCount.text=v;
        });
    }
    else  if(flag==3)
    {
        NSString *Code=[data1 objectForKey:@"Code"];
        
        NSString *HeadImg=[data1 objectForKey:@"HeadImg"];
        
        NSString *HeadImgName=[data1 objectForKey:@"HeadImgName"];
        
        NSString *isCheck=[data1 objectForKey:@"IsCheck"];
        
        dispatch_sync(dispatch_get_main_queue(), ^{
            self.lblUserCode.text=Code;
        });
        
        if([HeadImg isEqualToString:@""])
        {
            dispatch_sync(dispatch_get_main_queue(), ^{
                self.headImg.image= [UIImage imageNamed:@"2.jpg"];
                
                self.bgImgView.image=[UIImage imageNamed:@"2.jpg"];
            });
            
        }
        else
        {
            [Common downLoadImageByURL:self.headImg andURL:HeadImg andName:HeadImgName];
            
            [Common downLoadImageByURL:self.bgImgView andURL:HeadImg andName:HeadImgName];
        }
        
        if ([isCheck isEqualToString:@"0"])
        {
            dispatch_sync(dispatch_get_main_queue(), ^{
                self.imgCheckView.hidden=YES;
            });
        }
        else
        {
            dispatch_sync(dispatch_get_main_queue(), ^{
                self.imgCheckView.hidden=NO;
            });
        }
    }
}

-(void) stopNavBtnRoation
{
    [self stopNavRightBtnRoation];
    
    [self.marqueeView stopM];
    
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if([self musicPlay].isRoationing)
    {
        self.marqueeView.musicName= [self musicPlay].playingMusic.name;
        
        [self.marqueeView startM];
        
    }
    else
    {
        [self.marqueeView stopM];
    }
}

-(void) loadDJInfo
{
    NSString *djID=[Common getUserDefaults:@"djID"];
    
    [self loadData:djID];
    
}

//设置表格有几个数据区域
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//设置表格有几行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if([tableView isEqual:self.tableMenu])
    {
        return 6;
    }
    else  if([tableView isEqual:self.tableSet])
    {
        return 1;
    }
    return 0;
    
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 34;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if([tableView isEqual:self.tableMenu])
    {
        static NSString * const kCellIdentifier = @"mybtn";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
        
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier];
        }
        
        if(indexPath.row==0)
        {
            cell.textLabel.text = @"认证申请 - 富邦酒吧驻场DJ";
        }
        else  if(indexPath.row==1)
        {
            cell.textLabel.text = @"我的上传";
        }
        else  if(indexPath.row==2)
        {
            cell.textLabel.text = @"我的下载";
        }
        else  if(indexPath.row==3)
        {
            cell.textLabel.text = @"我的碟包";
        }
        else  if(indexPath.row==4)
        {
            cell.textLabel.text = @"我的金库";
        }
        else  if(indexPath.row==5)
        {
            cell.textLabel.text = @"我的专辑";
        }
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        return cell;
    }
    else if([tableView isEqual:self.tableSet])
    {
        static NSString * const kCellIdentifier = @"myset";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
        
        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier];
        }
        if(indexPath.row==0)
        {
            cell.textLabel.text = @"设置";
        }
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        return cell;
    }
    return  nil;
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = (UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    
    if([tableView isEqual:self.tableSet])
    {
        SetController *set=[self.storyboard instantiateViewControllerWithIdentifier:@"setting"];
        
        [self.navigationController pushViewController:set animated:YES];
    }
    else if([tableView isEqual:self.tableMenu])
    {
        if([cell.textLabel.text isEqualToString:@"我的下载"])
        {
            DownLoadInfoController  *downLoadInfo=[self.storyboard instantiateViewControllerWithIdentifier:@"downLoadInfo"];
            
            [downLoadInfo setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
            
            downLoadInfo.hidesBottomBarWhenPushed=YES;//隐藏底部菜单栏
            
            [self.navigationController pushViewController:downLoadInfo animated:YES];
        }
        else if([cell.textLabel.text containsString :@"认证申请"])
        {
            AuthenticationController  *authentication=[self.storyboard instantiateViewControllerWithIdentifier:@"Authentication"];
            
            [authentication setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
            
            authentication.hidesBottomBarWhenPushed=YES;//隐藏底部菜单栏
            
            [self.navigationController pushViewController:authentication animated:YES];
        }
        else if([cell.textLabel.text isEqualToString:@"我的金库"])
        {
            PointController  *point=[self.storyboard instantiateViewControllerWithIdentifier:@"point"];
            
            [point setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
            
            point.hidesBottomBarWhenPushed=YES;//隐藏底部菜单栏
            
            [self.navigationController pushViewController:point animated:YES];
        }
        else if([cell.textLabel.text isEqualToString:@"我的专辑"])
        {

        }
        else
        {

            ShowMusicListController *set=[self.storyboard instantiateViewControllerWithIdentifier:@"ShowMusicList"];
            
            set.pageTitle=cell.textLabel.text;
            
            set.hidesBottomBarWhenPushed=YES;//隐藏底部菜单栏
            
            [self.navigationController pushViewController:set animated:YES];

        }
        
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}



-(void) openUserInfo:(UITapGestureRecognizer *)sender
{
    UserInfoController *userInfoController=[self.storyboard instantiateViewControllerWithIdentifier:@"userinfo"];
    
    [userInfoController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
    
    userInfoController.hidesBottomBarWhenPushed=YES;//隐藏底部菜单栏
 
    [self.navigationController pushViewController:userInfoController animated:YES];
}


@end
